#include<stdio.h>
//用数组实现循环队列

#define MAXSIZE 5
typedef int ElemType;
typedef struct
{
    ElemType data[MAXSIZE];   //实际存储数据的空间只有MAXSIZE-1个，需要一个空间来判断空和满
    int front,rear;           //分别表示队头和队尾
}SqQueue;

//初始化循环队列
void SqQueue_init(SqQueue &sq)
{
    sq.front=sq.rear=0;   //初始化时都指向数组下标0位置
}

//判断队列空
bool SqQueue_empty(SqQueue &sq)
{
    return sq.front==sq.rear;
}

//入队
bool SqQueue_push(SqQueue &sq,ElemType x)
{
    //判断队列是否已满
    if( (sq.rear+1)%MAXSIZE == sq.front )
    {
        return false;
    }
    //从队尾入队
    sq.data[sq.rear]=x;
    //队尾指针需要往前移动
    sq.rear=(sq.rear+1)%MAXSIZE;
    return true;
}

//出队
bool SqQueue_pop(SqQueue &sq,ElemType &x)
{
    //判断队列是否为空
    if(SqQueue_empty(sq))
    {
        return false;
    }
    //从队头出
    x=sq.data[sq.front];
    //队头指针需往前移动
    sq.front=(sq.front+1)%MAXSIZE;
    return true;
}

int main() {
    //创建循环队列
    SqQueue sq;
    //初始化
    SqQueue_init(sq);

    bool flag;  //声明flag变量表示是否操作成功
    flag=SqQueue_empty(sq);
    if(flag)
    {
        printf("SqQueue is empty!\n");
    }
    else
    {
        printf("SqQueue is not empty!\n");
    }

    //入队列
    SqQueue_push(sq,1);
    SqQueue_push(sq,2);
    SqQueue_push(sq,3);
    flag=SqQueue_push(sq,4);
    if(flag)
    {
        printf("Successful push!\n");
    }
    else
    {
        printf("fail push!\n");
    }
    flag=SqQueue_push(sq,5);
    if(flag)
    {
        printf("Successful push!\n");
    }
    else
    {
        printf("fail push!\n");
    }

    ElemType x;    //声明出队列的元素
    SqQueue_pop(sq,x);
    SqQueue_pop(sq,x);
    SqQueue_pop(sq,x);
    flag=SqQueue_pop(sq,x);
    if(flag)
    {
        printf("Successful pop!\n");
    }
    else
    {
        printf("fail pop!\n");
    }
    flag=SqQueue_pop(sq,x);
    if(flag)
    {
        printf("Successful pop!\n");
    }
    else
    {
        printf("fail pop!\n");
    }

    return 0;
}
